package com.sanrenzu.everybodytv.ui.vertical.colum;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.sanrenzu.everybodytv.R;
import com.sanrenzu.everybodytv.bean.zxb.VideoItemChildBean;
import com.sanrenzu.everybodytv.ui.column.adapter.VideoItemChildAdapter;
import com.sanrenzu.everybodytv.ui.vertical.radio.RadioAcitivity;
import com.sanrenzu.everybodytv.widget.MyGirdView;
import com.sanrenzu.everybodytv.widget.VerticalScrollView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class ColumItemActivity extends AppCompatActivity implements ColumItemContract.View {

    @BindView(R.id.activity_columitem_gridbview)
    MyGirdView activityColumitemGridbview;
    @BindView(R.id.activity_columitem_title)
    TextView activityColumitemTitle;
    @BindView(R.id.activity_columitem_presh)
    VerticalScrollView activityColumitemPresh;
    @BindView(R.id.activity_columitem_back)
    ImageView activityColumitemBack;

    private ColumItemContract.Presenter presenter;
    private Unbinder bind;
    private String name = "";

    private VideoItemChildAdapter childAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_colum_item);
        bind = ButterKnife.bind(this);

        Intent intent = getIntent();
        name = intent.getStringExtra("name");

        presenter = new ColumItemPresenter(this);
        presenter.onGetVideoItem(name);

    }

    @Override
    public void onGetVideoItemChildBean(final List<VideoItemChildBean> videoItemChildBeen) {
        childAdapter = new VideoItemChildAdapter(ColumItemActivity.this, videoItemChildBeen);
        activityColumitemGridbview.setAdapter(childAdapter);
        activityColumitemTitle.setText(videoItemChildBeen.get(0).getCategoryName());

        childAdapter.notifyDataSetChanged();

        activityColumitemGridbview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String video = videoItemChildBeen.get(position).getUid();
                Intent intent = new Intent();
                intent.setClass(ColumItemActivity.this, RadioAcitivity.class);
                intent.putExtra("url", video);
                startActivity(intent);
            }
        });


        //设置刷新样式
        activityColumitemPresh.setPullLabel("下拉以刷新", PullToRefreshBase.Mode.PULL_FROM_START);
        activityColumitemPresh.setRefreshingLabel("正在加载。。。", PullToRefreshBase.Mode.PULL_FROM_START);
        activityColumitemPresh.setReleaseLabel("放开以刷新", PullToRefreshBase.Mode.PULL_FROM_START);

        activityColumitemPresh.setMode(PullToRefreshBase.Mode.PULL_FROM_START);

        activityColumitemPresh.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                new puToResh().execute();
            }
        });
    }

    @OnClick(R.id.activity_columitem_back)
    public void onClick() {
        finish();
    }


    //异步显示图片
    class puToResh extends AsyncTask {
        @Override
        protected Object doInBackground(Object[] params) {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Object o) {
            super.onPostExecute(o);
            activityColumitemPresh.onRefreshComplete();
        }
    }

    @Override
    public void onGetVideoItemBeanFailed(String error) {
        Log.i("TAG", "---->>VideoItemBeanFailed: " + error);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bind.unbind();
    }


}
